<template>
  <div class="ma-content-block  p-4">
    <!-- CRUD 组件 -->
    
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #tableBeforeButtons>
            <a-button type="primary" @click="addMoneyRef.open({type:1,dept_id:''}, () => {
              load()
          })">充值</a-button>
      </template>
      <template #change_money="{ record }">
        <div>
          <a-link v-if='record.change_money - 0 > 0' status='success'>+ {{record.change_money.toFixed(2)}}</a-link>
          <a-link v-else status='danger'>{{record.change_money.toFixed(2)}}</a-link>
        </div>
      </template>
      <template #now_money="{ record }">
        <div>
          {{record.now_money.toFixed(2)}}
        </div>
      </template>
      <template #money="{ record }">
        <div>
          {{record.money.toFixed(2)}}
        </div>
      </template>
      
      <template #summaryCell="{ column, record, rowIndex }">
        {{
          column.dataIndex == "dept_id"
            ? "小计"
            : column.dataIndex == "change_money"
            ? (record[column.dataIndex] - 0).toFixed(2)
            : record[column.dataIndex]
        }}
      </template>
    </ma-crud>
    <div class='lg:flex'>
      <!-- <div style='width:300px'>小计：{{xiaoji}}</div> -->
      <!-- <div>合计：{{heji}}</div> -->
    </div>
    <AddMoneyModal
      ref="addMoneyRef"
    />
  </div>
  
</template>
<script setup>
import { ref, reactive,computed } from 'vue'
import financialMoneyLog from '@/api/financial/financialMoneyLog'
import { Message } from '@arco-design/web-vue'
import { request } from "@/utils/request.js";
import tool from '@/utils/tool'
import * as common from '@/utils/common'
import AddMoneyModal from "@/views/financial/moneyCzLog/AddMoney.vue"

const addMoneyRef = ref()
const crudRef = ref()
const xiaoji = ref()
const heji = ref()
const load = () =>{
  crudRef.value.refresh()
}
const handleShenHe = async (id)=>{
  let checkuser = await handleZHONGjian(id)
  return checkuser.nickname
}

const handleZHONGjian = async (id)=>{
  let checkuser = await request({
        url: "/financial/transactions/index",
        method: "get",
        params: {
          page:1,
          pageSize:10,
          id
        },
      }).then((res) => {
        return res.data.items[0].checkuser
      });
    return checkuser
}


const options = reactive({
  id: 'flyos_financial_money_log',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: false,
  operationWidth: 160,
  searchColNumber:6,
  formOption: {
    viewType: 'modal',
    width: 600
  },
  showSummary: true,
  summary: [
    
    {
      dataIndex: "change_money",
      action: "sum",
    },
    
  ],
  api: financialMoneyLog.getList,
  add: {
    show: false,
    api: financialMoneyLog.save,
    auth: ['financial:moneyLog:save']
  },
  edit: {
    show: false,
    api: financialMoneyLog.update,
    auth: ['financial:moneyLog:update']
  },
  delete: {
    show: false,
    api: financialMoneyLog.deletes,
    auth: ['financial:moneyLog:delete']
  },
  export: {
    show: true,
    url: 'financial/moneyLog/export',
    auth: ['financial:moneyLog:export']
  },
  beforeRequest:(params)=>{
    params.event = 1
  },
  afterRequest:  (params) => {
      let num = 0
      params.map(async (v, i) => {
        v.now_money = v.now_money - 0
        v.change_money = v.change_money - 0
        v.money = ((v.now_money - 0) + (v.change_money - 0))
        num += (v.now_money - 0) + (v.change_money - 0)
        if(v.extend != null){
          v.transactions_id = v.extend.transactions_id
        }else{
          v.transactions_id = 0
        }
        let checkuser = await handleShenHe(v.transactions_id)
        v.nickname = checkuser
      })
      // request({
      //   url:'financial/moneyLog/total',
      //   method: 'get',
      //   params:{
            
      //   },
      // }).then(res=>{
      //   heji.value = res.message
      // })
      xiaoji.value = num.toFixed(2)
    },
})

const columns = reactive([
  {
    title: "",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入"
    }
  },
  {
    title: "公司",
    dataIndex: "dept_id",
    common: true,
    align: "center",
    width:180,
    search: true
  },
  {
    title: "类型",
    dataIndex: "event",
    formType: "select",
    align: "center",
    search: false,
    dict: {
      data: [
        {
          label: "充值",
          value: "1"
        },
        {
          label: "订单退款",
          value: "2"
        },
        {
          label: "装车订单扣款",
          value: "3"
        },
        {
          label: "预报到站扣款",
          value: "4"
        }
      ],
      translation: true
    }
  },
  
  {
    title: "充值前金额",
    dataIndex: "now_money",
    formType: "input",
    align: "center",
    // search: true
  },
  {
    title: "充值金额",
    dataIndex: "change_money",
    formType: "input",
    align: "center",
    // search: true
  },
  {
    title: "充值后金额",
    dataIndex: "money",
    formType: "input",
    align: "center",
    // search: true
  },
  {
    title: "充值时间",
    dataIndex: "created_at",
    formType: "range",
    addDisplay: false,
    editDisplay: false,
    showTime: true,
    search: true
  },
  {
    title: "审核人",
    dataIndex: "nickname",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
  },
  {
    title: "备注",
    dataIndex: "remark",
    align: "center",
    formType: "input",
    // search: true
  },
  {
    title: "更新时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "删除时间",
    dataIndex: "deleted_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "创建者",
    dataIndex: "created_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "更新者",
    dataIndex: "updated_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  }
])
</script>
<script> export default { name: 'financial:moneyCzLog' } </script>
